package hsp.admin.app.literature.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.literature.po.ArticlePO;
import hsp.admin.app.literature.service.IArticleService;
import hsp.admin.app.literature.vo.ArticleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "ArticleController", description = "文章管理")
@RestController
@RequestMapping("literature/article")
public class ArticleController {

    private final IArticleService service;

    @Autowired
    public ArticleController(
        IArticleService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取文章")
    @GetMapping("{id}")
    public ApiResult<ArticlePO> getArticle(@PathVariable("id") long id){
        ArticlePO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取文章分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<ArticleVO>> getArticlePage(@PathVariable int page, @PathVariable int size, ArticlePO po){
        Page<ArticleVO> records = service.pageVO(new Page<>(page, size), po);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑文章")
    @PostMapping()
    public ApiResult<?> postOrPutArticle(@RequestBody ArticlePO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除文章")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteArticle(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
